package com.jinlejia.erp.ui.mine.transfer;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.freedom.yefeng.yfrecyclerview.YfListInterface;
import com.freedom.yefeng.yfrecyclerview.YfListMode;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.BaseActivity;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.common.RecycleViewDivider;
import com.jinlejia.erp.databinding.ActivityTransferApplySearchBinding;
import com.jinlejia.erp.ui.mine.adapter.TransferApplySearchAdapter;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.Map;

import okhttp3.Call;

/**
 * 异动申请--搜索页面
 * 招募人 type =0,直属上级 type =1
 */
public class TransferApplySearchActivity extends BaseActivity {
    ArrayList<Map<String, Object>> historyList = new ArrayList();
    private TransferApplySearchAdapter adapter;
    private String type;
    private String newDutyCode = "";

    private ActivityTransferApplySearchBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_transfer_apply_search);
        initView();
        initData();
        initListener();
    }

    private void initView() {
        binding.yfrvSearchResult.setHasFixedSize(true);
        binding.yfrvSearchResult.setLayoutManager(new LinearLayoutManager(this));
        binding.yfrvSearchResult.addItemDecoration(new RecycleViewDivider(
                this, LinearLayoutManager.VERTICAL, 2, getResources().getColor(R.color.colorDivider)));
        adapter = new TransferApplySearchAdapter(historyList, binding.etSearchText.getText().toString().trim());
        binding.yfrvSearchResult.setAdapter(adapter);
        adapter.changeMode(YfListMode.MODE_DATA);
        binding.btClear.setOnClickListener(view -> {
            binding.etSearchText.setText("");
            getData();
        });

        binding.btBack.setOnClickListener(view -> finish());
    }

    private void initData() {
        type = getIntent().getStringExtra("type");
        if ("0".equals(type)) {
            binding.etSearchText.setHint("请输入招募人姓名");
        } else if ("1".equals(type)) {
            binding.etSearchText.setHint("请输入直属上级姓名");
            newDutyCode = getIntent().getStringExtra("newDutyCode").toString();
        }
    }

    private void initListener() {
        binding.etSearchText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                getData();
                if ("".equals(editable.toString())) {
                    binding.btClear.setVisibility(View.GONE);
                } else {
                    binding.btClear.setVisibility(View.VISIBLE);
                }
            }
        });

        adapter.setOnItemClickListener((view, o) -> {
            Intent intent = new Intent();
            intent.putExtra("pin", ((Map<String, Object>) o).get("pin").toString());//直属上级、招募人pin
            intent.putExtra("userName", ((Map<String, Object>) o).get("userName").toString());//直属上级、招募人名称
            if ("1".equals(type)) {
                intent.putExtra("shareStoreName", ((Map<String, Object>) o).get("shareStoreName").toString());//直属上级共享店名称
                intent.putExtra("shareStoreCode", ((Map<String, Object>) o).get("shareStoreCode").toString());//直属上级共享店编码
                intent.putExtra("dutyName", ((Map<String, Object>) o).get("dutyName").toString());//直属上级职务名称
            }
            setResult(RESULT_OK, intent);
            finish();
        });
    }

    private void getData() {
        if ("0".equals(type)) {//招募人
            newDutyCode = "";
        }
        OkHttpUtils
                .get()
                .url(ApiConfig.SUPERUSER__LIST)
                .addParams("newDutyCode", newDutyCode)//异动后职务编码
                .addParams("userName", binding.etSearchText.getText().toString())//搜索人员名称
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(TransferApplySearchActivity.this, response, adapter)) {
                            JSONObject jsonObject = JSON.parseObject(response.toString());
                            historyList = JSON.parseObject(jsonObject.getJSONArray("data").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            adapter.setData(historyList, binding.etSearchText.getText().toString().trim());
                        } else if (response.get("code").toString().equals("501")) {
                            adapter.changeMode(YfListMode.MODE_EMPTY);
                        }
                    }
                });
    }

}
